﻿var FreshFlower = new Object();

FreshFlower.GetCurrentLocation = function () {
    var url = window.location.href;
    var arr = url.split("/");
    return arr[0] + "//" + arr[2];
};

FreshFlower.ShowImageForPreview = function (imagePath, catId) {
    var imageUrl = FreshFlower.GetCurrentLocation() + "/SiteImages/" + catId + "/" + imagePath;
//    window.open(imageUrl);
    FreshFlower.loading(); // loading
    setTimeout(function () { // then show popup, deley in .5 second
        FreshFlower.LoadPopup(imageUrl); // function show popup 
    }, 500);
};

FreshFlower.popupStatus = 0;
FreshFlower.LoadPopup = function (imageUrl) {
    if (FreshFlower.popupStatus == 0) { // if value is 0, show popup
        FreshFlower.closeloading(); // fadeout loading

        $("#imgImagePreview").attr("src", imageUrl);

        var picRealWidth, picRealHeight;
        $("<img/>") // Make in memory copy of image to avoid css issues
            .attr("src", $("#imgImagePreview").attr("src"))
            .load(function () {
                picRealWidth = this.width;   // Note: $(this).width() will not
                picRealHeight = this.height; // work for in memory images.
            });

            setTimeout(function () {
                $("#toPopup").fadeIn(0500).width(picRealWidth + 20).height(picRealHeight + 40);
                $("#backgroundPopup").css("opacity", "0.7");
                $("#backgroundPopup").fadeIn(0001);
                FreshFlower.popupStatus = 1;
            }, 50);   
    }
};

FreshFlower.closeloading = function() {
    $("div.loader").fadeOut('normal');
};

FreshFlower.loading = function() {
    $("div.loader").show();
};

$("div.close").click(function () {
    FreshFlower.disablePopup();  // function close pop up
});

FreshFlower.disablePopup = function() {
    if (FreshFlower.popupStatus == 1) { // if value is 1, close popup
        $("#toPopup").fadeOut("normal");
        $("#backgroundPopup").fadeOut("normal");
        FreshFlower.popupStatus = 0; // and set value to 0
    }
};

$("div.close").hover(
					function () {
					    $('span.ecs_tooltip').show();
					},
					function () {
					    $('span.ecs_tooltip').hide();
					}
				);

$("div.close").click(function () {
    FreshFlower.disablePopup();  // function close pop up
});

$(this).keyup(function (event) {
    if (event.which == 27) { // 27 is 'Ecs' in the keyboard
        FreshFlower.disablePopup();  // function close pop up
    }
});

$("div#backgroundPopup").click(function () {
    FreshFlower.disablePopup();  // function close pop up
});